import apartmentApi, { AreaData } from "@/api/apartment"
import { defineStore } from "pinia"


type AreaSelectorStoreData = {
    provinceList:AreaData[],
    provinceIdName:string,
    provinceId:number | string,
    provinceName:string,
    cityList:AreaData[],
    cityIdName:string,
    cityId:number | string,
    cityName:string,
    districtList:AreaData[],
    districtIdName:string,
    districtId:number | string,
    districtName:string,
  }
  
  export let useAreaSelectorStore = defineStore("area",{
    state():AreaSelectorStoreData{
      return {
        provinceList:[],
        provinceIdName:'',
        provinceId:'',
        provinceName:'',
        cityList:[],
        cityIdName:'',
        cityId:'',
        cityName:'',
        districtList:[],
        districtIdName:'',
        districtId:'',
        districtName:''
      }
    },
    actions:{
      async getProvinceList(){
        let {data} = await apartmentApi.getProvinceList();
        this.provinceList = data
      },
      async getCityList(){
        this.cityIdName = "";
        this.districtIdName = "";
        let [id,name] = this.provinceIdName.split(":");
        this.provinceId = id;
        this.provinceName = name; 
        let {data} = await apartmentApi.getCityList(this.provinceId);
        this.cityList = data
      },
      async getDistrictList(){
        this.districtIdName = "";
        let [id,name] = this.cityIdName.split(":");
        this.cityId = id;
        this.cityName = name; 
        let {data} = await apartmentApi.getDistrictList(this.cityId);
        this.districtList = data
      },
      saveDistrictIdAndName(){
        let [id,name] = this.districtIdName.split(":");
        this.districtId = id;
        this.districtName = name;
      }
    }
  })